package zhaolu.com.example.Delicacy.MySQL;

import java.util.ArrayList;
import java.util.List;

import zhaolu.com.example.Delicacy.Entity.Manager;

public class ManagerDAO extends MySQLHelper {
    public List<Manager> getManagerList() {
        List<Manager> list = new ArrayList<>();
        try {
            getConnection();
            String sql = "select * from Manager";
            callableStatement = connection.prepareCall(sql);
            resultSet = callableStatement.executeQuery();
            if (resultSet.next()) {
                Manager item = new Manager();
                item.setId(resultSet.getString("idManager"));
                item.setPassword(resultSet.getString("password"));
                list.add(item);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            closeAll();
        }
        return list;
    }

    public Manager getManagerByUserAndPassword(String id, String password) {
        Manager item = null;
        try {
            getConnection();
            String sql = "select * from Manager where idManager=? and password=?";
            callableStatement = connection.prepareCall(sql);
            callableStatement.setString(1, id);
            callableStatement.setString(2, password);
            resultSet = callableStatement.executeQuery();
            if (resultSet.next()) {
                item = new Manager();
                item.setId(resultSet.getString("idManager"));
                item.setPassword(password);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            closeAll();
        }
        return item;
    }

    public int addManager(Manager item) {
        int row = 0;
        try {
            getConnection();
            String sql = "insert into Manager(idManager, password) values(?, ?)";
            callableStatement = connection.prepareCall(sql);
            callableStatement.setString(1, item.getId());
            callableStatement.setString(2, item.getPassword());
            row = callableStatement.executeUpdate();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            closeAll();
        }
        return row;
    }

    public int editManager(Manager item) {
        int iRow = 0;
        try {
            getConnection();
            String sql = "update Manager set password=? where idManager=?";
            callableStatement = connection.prepareCall(sql);
            callableStatement.setString(1, item.getPassword());
            callableStatement.setString(2, item.getId());
            iRow = callableStatement.executeUpdate();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            closeAll();
        }
        return iRow;
    }

    public int deleteManager(int id) {
        int iRow = 0;
        try {
            getConnection();
            String sql = "delete from Manager where idManger=?";
            callableStatement = connection.prepareCall(sql);
            callableStatement.setInt(1, id);
            iRow = callableStatement.executeUpdate();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            closeAll();
        }
        return iRow;
    }
}
